package cn.wolfcode.tieba.controller;

import cn.wolfcode.tieba.domain.User;
import cn.wolfcode.tieba.redis.service.IUserRedisService;
import cn.wolfcode.tieba.service.IUserService;
import cn.wolfcode.tieba.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * @author 王金龙
 * @date 2021/1/10 13:36
 */
@RestController
@RequestMapping("users")
public class UserController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IUserRedisService userRedisService;
    @PostMapping("/regist")
    public Object regist(String name,String password){
        userService.regist(new User(name,password));
        return JsonResult.success();
    }

    @PostMapping("/login")
    public Object login(String userName,String password) {
        User user = userService.login(userName,password);
        // 将令牌存入 redis
        String token = userRedisService.setLoginToken(user);
        HashMap<String, Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",user);
        return JsonResult.success(map);
    }

   
}
